package com.yimi.yinhepda.db.dao;

import android.database.Cursor;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.LoadUnLoadGroupTable;

import java.sql.SQLException;
import java.util.List;

/**
 * 装卸组表数据库操作类
 *
 * @author 000432
 * @date 2018/5/4 0004
 */

public class LoadUnloadGroupTableDao extends BaseDao<LoadUnLoadGroupTable, Long> {
    public LoadUnloadGroupTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), LoadUnLoadGroupTable.class);
    }

    public LoadUnLoadGroupTable getData() {
        List<LoadUnLoadGroupTable> list = findAll();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    public LoadUnLoadGroupTable createObject(Cursor cursor){
        LoadUnLoadGroupTable loadUnLoadGroupTable=null;
        try{
            if (cursor.moveToFirst()){
                loadUnLoadGroupTable=new LoadUnLoadGroupTable();
                loadUnLoadGroupTable.setId(cursor.getLong(cursor.getColumnIndex("id")));
                loadUnLoadGroupTable.setTask(cursor.getString(cursor.getColumnIndex("task")));
                loadUnLoadGroupTable.setPlatform(cursor.getString(cursor.getColumnIndex("platform")));
                loadUnLoadGroupTable.setWorkNums(cursor.getString(cursor.getColumnIndex("workNums")));
                loadUnLoadGroupTable.setUserNames(cursor.getString(cursor.getColumnIndex("userNames")));
                loadUnLoadGroupTable.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
                loadUnLoadGroupTable.setWorkTypes(cursor.getString(cursor.getColumnIndex("workTypes")));
                loadUnLoadGroupTable.setUserCodes(cursor.getString(cursor.getColumnIndex("userCodes")));
                if (cursor.getInt(cursor.getColumnIndex("palletBindCheck"))==0){
                    loadUnLoadGroupTable.setPalletBindCheck(false);
                }
                else{
                    loadUnLoadGroupTable.setPalletBindCheck(true);
                }

                loadUnLoadGroupTable.setCarNo(cursor.getString(cursor.getColumnIndex("carNo")));
            }
        }catch (Exception e){

        }
        finally {
            if (cursor!=null){
                cursor.close();
            }
        }
       return loadUnLoadGroupTable;
    }

    /**
     * 根据任务号判断数据是否存在
     */
    public boolean exitDataByTask(String task) {
        try {
            QueryBuilder<LoadUnLoadGroupTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);

            List<LoadUnLoadGroupTable> listUnloadScanTable = queryBuilder.query();
            if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public LoadUnLoadGroupTable getDataByTask(String task) {
        try {
            QueryBuilder<LoadUnLoadGroupTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void createOrUpdate(LoadUnLoadGroupTable loadUnLoadGroupTable) {
        try {
            LoadUnLoadGroupTable entity = getDataByTask(loadUnLoadGroupTable.getTask());
            if (entity == null) {
                insert(loadUnLoadGroupTable);
            } else {
                update(loadUnLoadGroupTable);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
